//
//  GWItemsMainViewController.m
//  GiftWanted
//
//  Created by lanou on 15/11/9.
//  Copyright © 2015年 陈文杰. All rights reserved.
//

#import "GWItemsMainViewController.h"
#import "MultilevelMenu.h"
#import "GWCategoriesModel.h"
#import "GWSubcategoriesModel.h"
#import "GWItemsDetailViewController.h"

@interface GWItemsMainViewController ()

@property (nonatomic, strong) MultilevelMenu *menu;
@property (nonatomic, strong) NSMutableArray *categoriesArr;
@property (nonatomic, strong) NSMutableArray *subcategoriesArr;

@end

@implementation GWItemsMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor yellowColor];
    [self requestData];
    
}

- (NSMutableArray *)categoriesArr {
    if (!_categoriesArr) {
        _categoriesArr = [NSMutableArray arrayWithCapacity:0];
    }
    return _categoriesArr;
}

- (NSMutableArray *)subcategoriesArr {
    if (!_subcategoriesArr) {
        _subcategoriesArr = [NSMutableArray arrayWithCapacity:0];
    }
    return _subcategoriesArr;
}

- (void)requestData {
    
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://api.liwushuo.com/v2/item_categories/tree"]]];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:nil];
        [self.categoriesArr removeAllObjects];
        for (NSDictionary *modelDic in dict[@"data"][@"categories"]) {
            GWCategoriesModel *model = [[GWCategoriesModel alloc] init];
            [model setValuesForKeysWithDictionary:modelDic];
            [self.categoriesArr addObject:model];
        }
        [self addMenu];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"网络请求失败");
    }];
    [operation start];
    
}

- (void)addMenu {
    
    NSMutableArray *lis=[NSMutableArray arrayWithCapacity:0];
    /**
     *  构建需要数据 2层或者3层数据 (ps 2层也当作3层来处理)
     */
    NSInteger countMax=_categoriesArr.count;
    for (int i=0; i<countMax; i++) {
        
        GWCategoriesModel *categoriesModel = _categoriesArr[i];
        rightMeun *meun=[[rightMeun alloc] init];
        meun.meunName=categoriesModel.name;
        NSMutableArray *sub=[NSMutableArray arrayWithCapacity:0];
        rightMeun *menu1= [[rightMeun alloc]init];
        menu1.meunName = categoriesModel.name;
        [sub addObject:menu1];
        NSMutableArray *zList = [NSMutableArray arrayWithCapacity:0];
        for (int z=0; z<categoriesModel.subcategories.count; z++) {
            
            GWSubcategoriesModel *subcategoriesModel = categoriesModel.subcategories[z];
            rightMeun *menu2 = [[rightMeun alloc]init];
            menu2.meunName = subcategoriesModel.name;
            menu2.urlName = subcategoriesModel.icon_url;
            menu2.meunNumber = i;
            menu2.ID = [NSString stringWithFormat:@"%d", z];
            [zList addObject:menu2];
        }
        menu1.nextArray=zList;
        meun.nextArray=sub;
        [lis addObject:meun];
    }

    _menu=[[MultilevelMenu alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-70) WithData:lis withSelectIndex:^(NSInteger left, NSInteger right,rightMeun* info) {
//        NSLog(@"%ld", info.meunNumber);
//        NSLog(@"%@", info.ID);
        NSInteger i = info.meunNumber;
        NSInteger j = info.ID.integerValue;
        GWCategoriesModel *model = _categoriesArr[i];
        GWSubcategoriesModel *subModel = model.subcategories[j];
        NSLog(@"%@", subModel.name);
        GWItemsDetailViewController *detailVC = [[GWItemsDetailViewController alloc] init];
        detailVC.id_s = subModel.id_s;
        detailVC.name = subModel.name;
        [self.naviC pushViewController:detailVC animated:YES];
    }];
    
    _menu.leftSelectColor = [UIColor colorWithRed:1.000 green:0.379 blue:0.396 alpha:1.000];
    //      view.leftSelectBgColor=[UIColor redColor];
    _menu.isRecordLastScroll=YES;
    [self.view addSubview:_menu];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
